package inet.app.view.adapter;

import inet.app.R;
import inet.app.controller.database.DataBaseHandler;
import inet.app.model.object.Veso_Item;
import inet.app.view.activity.VesoDetail;

import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListVesoAdapter extends ArrayAdapter<Veso_Item> {
	private DataBaseHandler db;	
	private Dialog dialog;
	private ArrayList<Veso_Item> items;
	private Activity context;
	Bundle rb = new Bundle();
	private ProgressDialog pdia;
	public ListVesoAdapter(Context context, ArrayList<Veso_Item> items) {
		super(context, R.layout.list_item_veso,items);
		this.items = items;
		this.context = (Activity) context;
	}
	
	public ListVesoAdapter(Context context, ArrayList<Veso_Item> items,ArrayList<String> item,ArrayList<String> path) {
		super(context, R.layout.list_item_veso,items);
		
		this.items = items;
		this.context = (Activity) context;
	}
	
	static class ViewHolder {
		protected TextView tvTinh,tvDate,tvBoso,tvKetqua;
		public Button btXemve,btXoave;
		public RelativeLayout rlitem;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		pdia= new ProgressDialog(getContext());
		View v = null;
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			v = inflater.inflate(R.layout.list_item_veso, null);
			final ViewHolder vh = new ViewHolder();
			vh.rlitem = (RelativeLayout) v.findViewById(R.id.rlitem);
			vh.tvTinh = (TextView) v.findViewById(R.id.tvTinh);
			vh.tvDate = (TextView) v.findViewById(R.id.tvDate);
			vh.tvBoso = (TextView) v.findViewById(R.id.tvBoso);
			vh.tvKetqua = (TextView) v.findViewById(R.id.tvKetqua);
			vh.btXemve =(Button) v.findViewById(R.id.btXemve);
			vh.btXoave =(Button) v.findViewById(R.id.btXoave);
			// Setonclick khi bấm vào view
			vh.btXemve.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					System.out.println("Vừa click vô đây ");
					pdia.show();
					Intent intent = new Intent (context, VesoDetail.class);
					rb.putString("code", items.get(position).getCode());
					rb.putString("date", items.get(position).getDate());
					rb.putString("listnumber", items.get(position).getListNumber());
					intent.putExtras(rb);
					context.startActivity(intent);
					pdia.dismiss();
				}
			});
			vh.btXoave.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int pos = position;
					db= new DataBaseHandler(context);
					db.deleteVeSo(Integer.parseInt(items.get(position).getListNumber()));
					vh.rlitem.removeAllViews();
				}
			});
			// End Setonclick
			v.setTag(vh);
		} else {
			v = convertView;
		}
		
		ViewHolder holder = (ViewHolder) v.getTag();
		holder.tvTinh.setText(items.get(position).getName());
		holder.tvDate.setText(items.get(position).getDate());
		holder.tvBoso.setText(items.get(position).getListNumber());
		holder.tvKetqua.setText(items.get(position).getKetqua());
		if(items.get(position).getKetqua().contains("Chưa có kết quả"))
		holder.btXemve.setEnabled(false);
		else holder.btXemve.setEnabled(true);
		
		return v;
	}
}
